package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月17日
*时间：下午6:45:06
*/
public class Q85 {
    /**
     * @param root: The root of the binary search tree.
     * @param node: insert this node into the binary search tree
     * @return: The root of the new binary search tree.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
        if (node == null) {
            return root;
        }
        if (root == null) {
            return node;
        }
        if (node.val > root.val) {
            if (root.right == null) {
                root.right = node;
            } else {
                insertNode(root.right, node);
            }
        } else {
            if (root.left == null) {
                root.left = node;
            } else {
                insertNode(root.left, node);
            }
        }
        return root;
    }

    @Test
    public void testA() {
        TreeNode root = new TreeNode(5);
        TreeNode r1 = new TreeNode(3);
        TreeNode r2 = new TreeNode(8);
        TreeNode r3 = new TreeNode(1);
        TreeNode r4 = new TreeNode(2);
        TreeNode r5 = new TreeNode(9);
        root.left = r1;
        root.right = r2;
        r1.left = r3;
        r2.right = r5;
        r3.right = r4;
        System.out.println(root);
        insertNode(root, new TreeNode(6));
        System.out.println(root);
    }
}
